package com.itheima.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.PageInfo;
import com.itheima.pojo.Student;
import com.itheima.pojo.User;
import com.itheima.service.ContactService;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/studentManage")
public class CaontactServlet extends BaseServlet {
    public void findInfo(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ContactService contactService = new ContactService();
        List<Student> list = contactService.findAll();
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageInfo<Student> studentPageInfo = new PageInfo<>(pageSize, list.size());
        BeanUtils.populate(studentPageInfo, req.getParameterMap());
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int startIndex = (currentPage - 1) * pageSize;
        List<Student> listByPage = contactService.findByPage(startIndex, pageSize);
        studentPageInfo.setTotalCount(list.size());
        studentPageInfo.setList(listByPage);
        int totalPage = studentPageInfo.getTotalPage();
        studentPageInfo.setTotalPage(totalPage);
        System.out.println(studentPageInfo);
        ObjectMapper om = new ObjectMapper();
        String strJson = om.writeValueAsString(studentPageInfo);
        resp.getWriter().print(strJson);
    }

    public void addStu(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ContactService contactService = new ContactService();
        Student stu = new Student();
        BeanUtils.populate(stu, req.getParameterMap());
        Boolean flag = contactService.addStu(stu);
        resp.getWriter().print(flag);
    }

    public void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        ContactService contactService = new ContactService();
        String number = req.getParameter("number");
        Boolean flag = contactService.delete(number);
        resp.getWriter().print(flag);
    }

    public void updateInfo(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ContactService contactService = new ContactService();
        Student stu = new Student();
        BeanUtils.populate(stu,req.getParameterMap());
        Boolean flag = contactService.update(stu);
        resp.getWriter().print(flag
        );
    }
}